﻿using Serilog;
using Serilog.Events;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using WinForm.NET4.Sample.Extensions;

namespace WinForm.NET4.Sample
{
    static class Program
    {
        /// <summary>
        /// 该函数设置由不同线程产生的窗口的显示状态。
        /// </summary>
        /// <param name="hWnd">窗口句柄</param>
        /// <param name="cmdShow">指定窗口如何显示。查看允许值列表，请查阅ShowWlndow函数的说明部分。</param>
        /// <returns>如果函数原来可见，返回值为非零；如果函数原来被隐藏，返回值为零。</returns>
        [DllImport("User32.dll")]
        private static extern bool ShowWindowAsync(IntPtr hWnd, int cmdShow);
        /// <summary>
        /// 该函数将创建指定窗口的线程设置到前台，并且激活该窗口。键盘输入转向该窗口，并为用户改各种可视的记号。系统给创建前台窗口的线程分配的权限稍高于其他线程。
        /// </summary>
        /// <param name="hWnd">将被激活并被调入前台的窗口句柄。</param>
        /// <returns>如果窗口设入了前台，返回值为非零；如果窗口未被设入前台，返回值为零。</returns>
        [DllImport("User32.dll")]
        private static extern bool SetForegroundWindow(IntPtr hWnd);
        private const int WS_SHOWMAXIMIZE = 3;  //最大化显示窗口
        private const int WS_SHOW = 5;//以原来大小和位置显示
       
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(true);

            //处理主线程异常
            Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);

            //处理其他线程的异常
            AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler(CurrentDomain_UnhandledException);
            //设置Log配置
            SetupLogger();

            //只允许单个实例运行
            Process instance = RunningInstance();

            if (instance == null)
            {

                Login login = new Login();

                if (login.ShowDialog() == DialogResult.Yes)
                {
                    Application.Run(new Shell());

                }

            }
            else
            {
                HandleRunningInstance(instance);

            }

        }

        /// <summary>
        /// 获取正在运行的实例，没有运行的实例返回null;
        /// </summary>
        static Process RunningInstance()
        {
            Process current = Process.GetCurrentProcess();
            Process[] processes = Process.GetProcessesByName(current.ProcessName);
            foreach (Process process in processes)
            {
                if (process.Id != current.Id)
                {
                    if (process.MainModule.FileName == current.MainModule.FileName)
                    {
                        return process;
                    }
                }
            }
            return null;
        }

        /// <summary>
        /// 显示已运行的程序。
        /// </summary>
        static void HandleRunningInstance(Process instance)
        {

            ShowWindowAsync(instance.MainWindowHandle, WS_SHOWMAXIMIZE); //最大显示，WS_SHOW 正常显示
          
            SetForegroundWindow(instance.MainWindowHandle); //放到前端
        }

        /// <summary>
        /// 配置Log
        /// </summary>
        static void SetupLogger()
        {

            Log.Logger = new LoggerConfiguration()
                .MinimumLevel.Debug()
                .MinimumLevel.Override("Microsoft", LogEventLevel.Error)
                .Enrich.FromLogContext()
                .Enrich.WithLoginInfo()//显示自定义的log信息
                .WriteTo.Logger(x => x.Filter.ByIncludingOnly(e => e.Level == LogEventLevel.Error || e.Level == LogEventLevel.Fatal)
                    .WriteTo.File(@"Logs/Error.log",
                        outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] {Message:lj}{NewLine}User Name: {UserName}{NewLine}User Id: {UserId}{NewLine}Company Name: {CompanyName}{NewLine}{Exception}",
                        fileSizeLimitBytes: 1_000_000,
                        rollOnFileSizeLimit: true,
                        shared: true,
                        flushToDiskInterval: TimeSpan.FromSeconds(1))
                )
                .WriteTo.Logger(x => x.Filter.ByIncludingOnly(e => e.Level == LogEventLevel.Warning)
                    .WriteTo.File(@"Logs/Warning.log",
                        fileSizeLimitBytes: 1_000_000,
                        rollOnFileSizeLimit: true,
                        shared: true,
                        flushToDiskInterval: TimeSpan.FromSeconds(1))
                )
                .WriteTo.Logger(x => x.Filter.ByIncludingOnly(e => e.Level == LogEventLevel.Information)
                    .WriteTo.File(@"Logs/Info.log",

                        fileSizeLimitBytes: 1_000_000,
                        rollOnFileSizeLimit: true,
                        shared: true,
                        flushToDiskInterval: TimeSpan.FromSeconds(1))
                )
                .WriteTo.Logger(x => x.Filter.ByIncludingOnly(e => e.Level == LogEventLevel.Debug)
                    .WriteTo.File(@"Logs/Debug.log",
                        fileSizeLimitBytes: 1_000_000,
                        rollOnFileSizeLimit: true,
                        shared: true,
                        flushToDiskInterval: TimeSpan.FromSeconds(1))
                )
                //也可以自动发送log信息到指定的邮箱
                //.WriteTo.Email(new EmailConnectionInfo{
                //                    FromEmail = ConfigurationManager.AppSettings["FromAddress"],
                //                    ToEmail = ConfigurationManager.AppSettings["ToAddress"],
                //                    MailServer = ConfigurationManager.AppSettings["MailServer"],
                //                    NetworkCredentials = new NetworkCredential
                //                    {
                //                        UserName = ConfigurationManager.AppSettings["Username"],
                //                        Password = ConfigurationManager.AppSettings["Password"]
                //                    },
                //                    EnableSsl = true,
                //                    Port = 465,
                //                    EmailSubject = ConfigurationManager.AppSettings["EmailSubject"]
                //                },
                //                outputTemplate: "{Timestamp:yyyy-MM-dd HH:mm:ss.fff zzz} [{Level:u3}] {Message:lj}{NewLine}User Name: {UserName}{NewLine}User Id: {UserId}{NewLine}Company Name: {CompanyName}{NewLine}{Exception}",
                //                batchPostingLimit: 10,
                //                restrictedToMinimumLevel: Serilog.Events.LogEventLevel.Error
                //                )
                .CreateLogger();
        }

        /// <summary>
        /// 处理主线程异常
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
        {
            // All exceptions thrown by the main thread are handled over this method
            CatchExceptionDetails(e.Exception);
        }

        /// <summary>
        /// 处理其他线程的异常
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        static void CurrentDomain_UnhandledException(object sender, UnhandledExceptionEventArgs e)
        {
            // All exceptions thrown by additional threads are handled in this method
            CatchExceptionDetails(e.ExceptionObject as Exception);
        }

        /// <summary>
        /// Log 错误信息，并弹框警示
        /// </summary>
        /// <param name="ex"></param>
        static void CatchExceptionDetails(Exception ex)
        {
            // Do logging of exception details
            Log.Error(ex, "");

            var message = String.Format("对不起, 系统出错了.\r\n" + "{0}\r\n" + "{1}\r\n" + "请联系技术人员.", ex.Message, ex.StackTrace);
            MessageBox.Show(message, @"未知错误", MessageBoxButtons.OK, MessageBoxIcon.Error);



        }
    }
}
